package com.lzh.farmersshop.controller.mall;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lzh.farmersshop.entity.Admininfo;
import com.lzh.farmersshop.entity.Userinfo;
import com.lzh.farmersshop.service.UserinfoService;
import com.lzh.farmersshop.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * (Userinfo)表控制层
 *
 * @author makejava
 * @since 2024-01-04 15:41:51
 */
@Slf4j
@RestController
@RequestMapping("mall/userinfo")
@Transactional(rollbackFor = Exception.class)
public class UserController {
    /**
     * 服务对象
     */
    @Resource
    private UserinfoService userinfoService;

    /**
     * 登录
     * @param phone
     * @param password
     * @param request
     * @return
     */
    @PostMapping("login")
    public Result login(String phone, String password, HttpServletRequest request) {
        //判断参数是否为空
        if(phone==null||password==null){
            return Result.failure("手机号和密码不能为空");
        }

        //根据账号查找
        QueryWrapper<Userinfo> user = new QueryWrapper<>();
        user.eq("phone",phone);
        Userinfo userinfo = userinfoService.getOne(user);
        if(userinfo==null){
            return Result.failure("账号不存在");
        }

        if(!userinfo.getPassword().equals(password)){
            return Result.failure("密码错误");
        }

        // 获取Session对象
        HttpSession session = request.getSession();

        // 将用户信息存储到Session中
        session.setAttribute("userinfo", userinfo);
        return Result.success(userinfo.getUsername());
    }

    /**
     * 注册
     * @param phone
     * @param password
     * @return
     */
    @PostMapping("register")
    public Result register(String phone, String password) {
        //判断参数是否为空
        if(phone==null||password==null){
            return Result.failure("手机号和密码不能为空！！！");
        }

        //判断手机号是否存在
        QueryWrapper<Userinfo> user = new QueryWrapper<>();
        user.eq("phone",phone);
        Userinfo userinfo = userinfoService.getOne(user);
        if(userinfo!=null){
            return Result.failure("手机号已存在，请直接登录！！！");
        }

        userinfo = new Userinfo();
        userinfo.setPhone(phone);
        userinfo.setPassword(password);
        userinfo.setIsdelete(1);
        userinfo.setUsername("用户"+phone);

        if (userinfoService.save(userinfo)){
            return Result.success("注册成功");
        }
        return Result.failure("注册失败");
    }


    /**
     * 退出
     * @param request
     * @return
     */
    @GetMapping("logout")
    public Result logout(HttpServletRequest request) {
        // 获取Session对象
        HttpSession session = request.getSession();

        // 将用户信息从Session中删除
        session.removeAttribute("userinfo");
        return Result.success("退出成功");
    }

    /**
     * 修改用户信息
     */
    /**
     * 修改数据
     *
     * @param userinfo 实体对象
     * @return 修改结果
     */
    @PostMapping("update")
    public Result update(Userinfo userinfo,HttpServletRequest request) {
        //获取用户信息
        if (request.getSession().getAttribute("userinfo")== null) {
            return Result.failure(401,"请先登录！！！");
        }

        if (userinfoService.updateById(userinfo)){
            request.getSession().setAttribute("userinfo", userinfo);
            return Result.success("修改成功");
        }
        return Result.success("修改失败");
    }

    /**
     * 根据id查询用户信息
     */
    @GetMapping("yzUserPasswordById")
    public Result yzUserPasswordById(Integer id,HttpServletRequest request,String oldPassword) {
        //获取用户信息
        if (request.getSession().getAttribute("userinfo")== null) {
            return Result.failure(401,"请先登录！！！");
        }
        //获取用户信息
        if (id==null) {
            return Result.failure("参数获取失败！！！");
        }

        Userinfo userinfo = userinfoService.getById(id);
        if (userinfo==null) {
            return Result.failure("用户不存在！！！");
        }
        if (!StringUtils.isEmpty(oldPassword)&&!userinfo.getPassword().equals(oldPassword)) {
            return Result.failure("密码错误！！！");
        }
        return Result.success("验证通过");
    }

}

